package com.jmt.goods.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jmt.goods.dao.GoodsTypeDao;
import com.jmt.goods.po.GoodsType;
import com.jmt.system.db.DbConn;

public class GoodsTypeDaoImpl implements GoodsTypeDao {
	private Connection conn ;
	
	@Override
	public boolean addGoodsType(GoodsType goodsType) {
		try {
			conn = DbConn.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("INSERT INTO  T_GoodsType VALUES(SEQ_T_GOODSTYPE.nextVal,?,?,?,?,?,?,?,?,?)");
			pstmt.setString(1, goodsType.getTypeName());
			pstmt.setString(2, goodsType.getTypeDesc());
			pstmt.setBoolean(3, goodsType.isLeaf());
			pstmt.setInt(4, goodsType.getParentId());
			pstmt.setInt(5, goodsType.getTypeLevel());
			pstmt.setInt(6, goodsType.getCreaterId());
			pstmt.setDate(7, new Date(goodsType.getCreateDate().getTime()));
			pstmt.setInt(8, goodsType.getUpdaterId());
			pstmt.setDate(9, new Date(goodsType.getUpdateDate().getTime()));
			int result = pstmt.executeUpdate();
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		
	}

	@Override
	public boolean deleteGoodsType(int goodsTypeId) {
		try {
			conn = DbConn.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("DELETE FROM T_GoodsType WHERE TypeID = ?");
			pstmt.setInt(1, goodsTypeId);
			int result = pstmt.executeUpdate();
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		
	}

	@Override
	public boolean updateGoodsType(int goodsTypeId, String data) {
		try {
			conn = DbConn.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("UPDATE T_GoodsType SET "+data+" WHERE TypeID = ?");
			pstmt.setInt(1, goodsTypeId);
			int result = pstmt.executeUpdate();
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public List<GoodsType> getAllGoodsType() {
		List<GoodsType> goodsTypeList = new ArrayList<GoodsType>();
		try {
			conn = DbConn.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM T_GoodsType ");
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				GoodsType goodsType = new GoodsType();
				goodsType.setTypeId(rs.getInt(1));
				goodsType.setTypeName(rs.getString(2));
				goodsType.setTypeDesc(rs.getString(3));
				goodsType.setLeaf(rs.getBoolean(4));
				goodsType.setParentId(rs.getInt(5));
				goodsType.setTypeLevel(rs.getInt(6));
				goodsType.setCreaterId(rs.getInt(7));
				goodsType.setCreateDate(rs.getDate(8));
				goodsType.setUpdaterId(rs.getInt(9));
				goodsType.setUpdateDate(rs.getDate(10));
				goodsTypeList.add(goodsType);
			}
			return goodsTypeList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public GoodsType getGoodsTypeById(int goodsTypeId) {
		try {
			conn = DbConn.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM T_GoodsType WHERE TypeID = ?");
			pstmt.setInt(1, goodsTypeId);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				GoodsType goodsType = new GoodsType();
				goodsType.setTypeId(rs.getInt(1));
				goodsType.setTypeName(rs.getString(2));
				goodsType.setTypeDesc(rs.getString(3));
				goodsType.setLeaf(rs.getBoolean(4));
				goodsType.setParentId(rs.getInt(5));
				goodsType.setTypeLevel(rs.getInt(6));
				goodsType.setCreaterId(rs.getInt(7));
				goodsType.setCreateDate(rs.getDate(8));
				goodsType.setUpdaterId(rs.getInt(9));
				goodsType.setUpdateDate(rs.getDate(10));
				return goodsType;
			}
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<GoodsType> getGoodsTypeByCondition(String condition) {
		List<GoodsType> goodsTypeList = new ArrayList<GoodsType>();
		try {
			conn = DbConn.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM T_GoodsType WHERE "+condition);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				GoodsType goodsType = new GoodsType();
				goodsType.setTypeId(rs.getInt(1));
				goodsType.setTypeName(rs.getString(2));
				goodsType.setTypeDesc(rs.getString(3));
				goodsType.setLeaf(rs.getBoolean(4));
				goodsType.setParentId(rs.getInt(5));
				goodsType.setTypeLevel(rs.getInt(6));
				goodsType.setCreaterId(rs.getInt(7));
				goodsType.setCreateDate(rs.getDate(8));
				goodsType.setUpdaterId(rs.getInt(9));
				goodsType.setUpdateDate(rs.getDate(10));
				goodsTypeList.add(goodsType);
			}
			return goodsTypeList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

}
